﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;

namespace Workflow.Alerts.Sublayouts.ProfileSettings
{
    public partial class ContactPersonDtls : BaseControl, IPersonView
    {
        #region Fields
        int id;
        #endregion

        #region IPersonView Members

        public string NetworkOperatorName
        {
            set { lblTitle.Text = string.Format("Contactpersoon voor {0}", value); }
        }

        public string FirstName
        {
            set { txtFirstName.Text = value; }
            get { return txtFirstName.Text; }
        }

        public string LastName
        {
            set { txtLastName.Text = value; }
            get { return txtLastName.Text; }
        }

        public string Phone1
        {
            set { txtPhone1.Text = value; }
            get { return txtPhone1.Text; }
        }

        public string Phone2
        {
            set { txtPhone2.Text = value; }
            get { return txtPhone2.Text; }
        }

        public string Email
        {
            set { txtEmail.Text = value; }
            get { return txtEmail.Text; }
        }

        public string Login
        {
            set { txtLogin.Text = value; }
            get { return txtLogin.Text; }
        }

        public string Password
        {
            set { txtPassword.Text = value; }
            get { return txtPassword.Text; }
        }

        public int? Id
        {
            get
            {
                int id;
                if (Sitecore.Context.Item.Name == "MyProfile")
                {
                    return (Session[WorkflowConstants.CurrentUser] as User).Id;
                }
                if (Request.QueryString["uId"] != null &&
                    Int32.TryParse(Request.QueryString["uId"], out id))
                {
                    return id;
                }
                return null;
            }
        }

        public bool Notifications
        {
            get { return chkNotification.Checked; }
            set { chkNotification.Checked = value; }
        }

        public bool Confirmations
        {
            get { return chkConfirmation.Checked; }
            set { chkConfirmation.Checked = value; }
        }

        public int? NetworkOperatorId
        {
            get
            {
                //if (Request.QueryString["opId"] != null && Int32.TryParse(Request.QueryString["opId"], out id))
                //{
                //    return id;
                //}
                //else if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.NetworkOperator)
                //{
                //    return (Session[WorkflowConstants.CurrentUser] as User).TypeId;
                //}
                if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.NetworkOperator)
                {
                    return (Session[WorkflowConstants.CurrentUser] as User).TypeId;
                }
                if (Request.QueryString["opId"] != null && Int32.TryParse(Request.QueryString["opId"], out id))
                {
                    return id;
                }
                return null;
            }
        }

        public string ExceptionField { get; set; }

        public event IPersonViewLoad OnPageLoad;
        public event IPersonViewUpdate OnDataUpdate;

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            lblEmailValidator.Visible = false;
            lblLoginValidator.Visible = false;
            PresentationManager.RegisterView(typeof(PersonDtlsPresenter), this, new WebSessionProvider());

            Sitecore.Data.Fields.MultilistField visibleFields = (Sitecore.Data.Fields.MultilistField)Sitecore.Context.Item.Fields["PersonVisibleFields"];
            if (visibleFields != null)
            {
                var fields = visibleFields.GetItems();
                foreach (Item item in fields)
                {
                    var field = item.Name;
                    var control = this.FindControlRecursive(field);
                    if (control != null)
                        control.Visible = true;
                }

                lblAccountInfo.Visible = (login.Visible || password.Visible);
            }

            if (Sitecore.Context.Item.Name == "MyProfile")
            {
                btnSaveNew.Visible = false;
            }

            bool idHasValue = Id.HasValue;
            if (!IsPostBack && idHasValue && OnPageLoad != null)
            {
                OnPageLoad();
            }

            txtLogin.Enabled = !idHasValue;
            password.Visible = !idHasValue;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (OnDataUpdate != null)
            {
                if (OnDataUpdate())
                {
                    if (Sitecore.Context.Item.Name == "MyProfile")
                    {
                        OnPageLoad();
                    }
                    else
                    {
                        Item previtem = Sitecore.Context.Item.Parent;
                        var path = Sitecore.Links.LinkManager.GetItemUrl(previtem);
                        if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.NetworkOperator)
                        {
                            Response.Redirect(path);
                        }
                        else
                        {
                            Response.Redirect(GetNetworkId(path));
                        }
                    }
                }
                else
                {
                    if (ExceptionField == "login")
                        lblLoginValidator.Visible = true;
                    else if (ExceptionField == "email")
                        lblEmailValidator.Visible = true;
                }
            }
        }

        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid)
            {
                if (OnDataUpdate != null)
                {
                    OnDataUpdate();
                }

                var path = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item);
                if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.NetworkOperator)
                {
                    Response.Redirect(path);
                }
                else
                {
                    Response.Redirect(GetNetworkId(path));
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Sitecore.Context.Item.Name == "MyProfile")
            {
                OnPageLoad();
                return;
            }
            var path = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item.Parent);
            if ((Session[WorkflowConstants.CurrentUser] as User).Type == WorkflowEnums.UserType.NetworkOperator)
            {
                Response.Redirect(path);
            }
            else
            {
                Response.Redirect(GetNetworkId(path));
            }
        }

        #endregion

        #region Private methods
        private string GetNetworkId(string path)
        {
            return string.Format("{0}?opId={1}", path, NetworkOperatorId);
        }
        #endregion


    }
}